<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//订单模型
class Orders_model extends CI_Model{
	const TBL_ORDERS = 'orders';
    //日志表
    const TBL_LOGS = 'logs';

    #添加订单，注意返回值,是新插入记录的id
    public function add_order($data){
        $query = $this->db->insert(self::TBL_ORDERS,$data);
        return $query ? $this->db->insert_id() : false;
    }

    #添加日志，注意返回值,是新插入记录的id
    public function add_logs($data){
        $query = $this->db->insert(self::TBL_LOGS,$data);
        return $query ? $this->db->insert_id() : false;
    }

    #获取单条订单信息
    public function get_order_tradeno($trade_no){
        $condition['trade_no'] = $trade_no;
        $query = $this->db->where($condition)->get(self::TBL_ORDERS);
        #返回单条记录
        return $query->row_array();
    }

    #更新
    public function update_order($data,$id){
        $condition['id'] = $id;
        return $this->db->where($condition)->update(self::TBL_ORDERS,$data);
    }


    #根据用户user_id查看订单信息
    public function list_user_order($limit,$offset,$user_id,$pay_status=NULL){
        $condition['user_id'] = $user_id;
        $this->db->limit($limit,$offset);
        if(isset($pay_status)){
            $condition['pay_status'] = $pay_status;
        }
        $query = $this->db->order_by('ctime desc')->where($condition)->get(self::TBL_ORDERS);
        return $query->result_array();
    }

    public function count_user_order($user_id,$pay_status=NULL){
        $condition['user_id'] = $user_id;
        if(isset($pay_status)){
            $condition['pay_status'] = $pay_status;
        }
        return $this->db->where($condition)->count_all_results(self::TBL_ORDERS);
    }


}